package com.imooc.hadoop.mapreduce.topN;

import com.imooc.hadoop.mapreduce.wordsCount.WordsCountMapper;
import com.imooc.hadoop.mapreduce.wordsCount.WordsCountReducer;
import com.imooc.hadoop.mapreduce.wordsCount.WorldsCount;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * @program: hadoop
 * @description: TopNJob
 * @author: zengchen
 * @create: 2021-03-30 15:25
 **/
public class TopNJob {

    public static final String KEY_NAME = "topN";

    public static void main(String[] args) throws Exception {
        if (args.length < 2) {
            System.exit(100);
        }
        Configuration conf = new Configuration();
        String nums = "10";
        if (StringUtils.isNotBlank(args[2])) {
            nums = args[2];
        }
        conf.set(KEY_NAME, nums);
        Job job = Job.getInstance(conf);
        job.setJarByClass(TopNJob.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.setMapperClass(TopNMapper.class);
        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(NullWritable.class);

        job.setReducerClass(TopNReducer.class);
        job.setOutputKeyClass(LongWritable.class);
        job.setOutputValueClass(NullWritable.class);

        job.waitForCompletion(true);
    }
}
